layui.use(['jquery','element','layer'], function () {
	$ = layui.$;
	var element = layui.element;
	var layer = layui.layer;

	$(document).keypress(function (e) { // 回车键事件
	    if (e.which == 13) {
	        $('.layui-form-item button.submit').click();
	    }
	});

	$('.validate-code').on('click', createCode).click();
	// 点击登录
	$('.layui-form-item').on('click', 'button', function(){
		if ( $(this).hasClass('submit') && $(this).attr('checked') ) {
			validateCode();
		} else if ( $(this).hasClass('reset') ) {
			reset();
		}
	});
});
localStorage.serverHttp = 'http://172.31.101.14:8090';
// localStorage.serverHttp = 'http://172.31.100.27:8081';
var code;
function createCode() {
    code = "";
    var codeLength = 4; //验证码的长度
    var checkCode = document.getElementById("validate-code");
    var codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //所有候选组成验证码的字符，当然也可以用中文的
    for (var i = 0; i < codeLength; i++) {
        var charNum = Math.floor(Math.random() * 52);
        code += codeChars[charNum];
    }
    if (checkCode) {
        checkCode.innerHTML = code;
    }
}
function reset() {
	$('.user-name input[name=account]').val('');
	$('.password input[name=pwd]').val('');
	$('#input-code').val('');
}

function validateCode() {
	var elem 		= document.getElementById("input-code");
	var inputCode 	= elem.value;
	var userName 	= document.querySelector('.user-name input[name=account]');
	var password 	= document.querySelector('.password input[name=pwd]');
	var subBtn		= document.querySelector('.layui-form-item button.submit');
	if ( !userName.value.replace(/(^\s*)|(\s*$)/g, "") ) {
		layer.msg('用户名不能为空！');
		userName.value = '';
		userName.select();
		subBtn.setAttribute('checked', true);
	    return false;
	} else if ( !password.value.replace(/(^\s*)|(\s*$)/g, "") ) {
		layer.msg('密码不能为空！');
		password.value = '';
		password.select();
		subBtn.setAttribute('checked', true);
	    return false;
	} else if (inputCode.length <= 0) {
	    layer.msg("请输入验证码！");
	    elem.select();
	    subBtn.setAttribute('checked', true);
	    return false;
	} else if (inputCode.toUpperCase() != code.toUpperCase()) {
	    layer.msg("验证码输入有误！");
	    elem.select();
	    createCode();
	    return false;
	} else {
		subBtn.removeAttribute('checked');
	    $.ajax({
	    	url: localStorage.serverHttp+'/base/login',
	    	type: 'post',
	    	dataType: 'json',
	    	data: {
	    		username: $('.user-name input[name=account]').val(),
	    		password: $('.password input[name=pwd]').val()
	    	}
	    })
	    .done(function(res) {
	    	if ( res.status && 1 === res.status ) {
	    		layer.msg('登录成功！');
	    		localStorage.user = JSON.stringify(res.data.userData);
	    		localStorage.menu = JSON.stringify(res.data.menuData);
	    		localStorage.token = JSON.stringify(res.data.token);
	    		setTimeout(function(){
	    			location.href = location.href.substr(0, location.href.lastIndexOf('/login'));
	    		}, 1000);
	    	} else if ( res.status && 1 !== res.status ) {	// 登录错误
	    		layer.msg(res.msg);
	    	}
	    })
	    .fail(function(res) {	// 请求错误
	    	layer.msg(res.msg);
	    })
	    .always(function() {
	    	subBtn.setAttribute('checked', true);
	    });
	    return false;
	}  
}

